package com.happiness.base.utils.redis.stand;

import cn.hutool.core.util.StrUtil;
import com.happiness.base.properties.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;

/**
 * 单机版redis配置
 * */
public class RedisConfig extends RedisProperties {


    /**
     * 单机版配置
     * @Title: JedisConnectionFactory
     * @param @param jedisPoolConfig  jedis连接池配置
     * @param @return
     * @return JedisConnectionFactory
     */
    @Bean
    public JedisConnectionFactory JedisConnectionFactory(JedisPoolConfig jedisPoolConfig){
        Duration duration = Duration.ofMillis(super.getTimeout());
        JedisClientConfiguration jedisClientConfiguration = JedisClientConfiguration.builder()
                .usePooling().poolConfig(jedisPoolConfig).and()
                .connectTimeout(duration)
                .readTimeout(duration).build();
        RedisStandaloneConfiguration redisConfig = new RedisStandaloneConfiguration(super.getHost(),super.getPort());
        if (StrUtil.isNotEmpty(super.getPassword())){
            redisConfig.setPassword(super.getPassword());
        }
        redisConfig.setDatabase(super.getDatabase());
        JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory(redisConfig,jedisClientConfiguration);
        return JedisConnectionFactory;
    }

}
